package com.xudow.app.newui.task;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.activeshare.edu.ucenter.common.messages.course.CourseDetailMessage;
import com.google.gson.Gson;
import com.xudow.app.Config;
import com.xudow.app.network.AjaxCallBack;
import com.xudow.app.network.OkHttpClientManager;
import com.xudow.app.newui.bean.CourseDetaills;
import com.xudow.app.newui.zutils.LsUtils;
import com.xudow.app.ui.CourseDetailActivity;

import java.util.HashMap;
import java.util.Map;

import static com.xudow.app.util.LogUtils.LOGE;

/**
 * 作者：LownChen on 2016/10/21 11:42
 */
public class okCourseDetailTask {


    private static final String COURSE_DETAIL_URL = Config.BASE_URL + Config.COURSE_DETAIL_URL;

    CourseDetailActivity courseDetailActivity;
    Handler courseDetailHandler;
    Long courseId;

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;

    public okCourseDetailTask(CourseDetailActivity courseDetailActivity, Handler courseDetailHandler, Long courseId) {
        this.courseDetailActivity=courseDetailActivity;
        this.courseDetailHandler=courseDetailHandler;
        this.courseId=courseId;
    }

    public void cd(){

        OkHttpClientManager ok=new OkHttpClientManager();
        Map<String,String> map=new HashMap<>();
        map.put("courseid", "" + courseId);
        ok.getAsyn(courseDetailActivity, COURSE_DETAIL_URL, map, new AjaxCallBack() {
            @Override
            public void onSucces(String response) throws Exception {

//                Log.i("courseDetailHandler","onSuccesresponse>>"+response.toString());
                CourseDetaills result = new Gson().fromJson(response,
                        CourseDetaills.class);
//                LsUtils.pdDismiss();
//                Log.i("courseDetailHandler","result>>"+result.toString());
                if (result != null) {

                    //FileUtils.writeFile(result1,new File(context.getCacheDir().getAbsolutePath(),"coursedetail.txt"));
                    if (result.getResult().equals("0")) {
                        sendSuccessMessage(result);
//                        Log.i("courseDetailHandler", "else>>0");
                    } else if (result.getResult().equals("1")) {
//                        Log.i("courseDetailHandler","else>>1");
                        sendErrorMessage(result.getErrorMessage());

                    }
                } else {
//                    Log.i("courseDetailHandler","else>>null");
                    sendErrorMessage(null);
                 }

            }

            @Override
            public void onFail(String response) {
//                Log.i("courseDetailHandler","response>>"+response.toString());
            }
        });
    }

    private void sendErrorMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = ERROR;

        if (null != msg) {
            Bundle data = new Bundle();
            data.putString("errMessage", msg);
            message.setData(data);
        }

        courseDetailHandler.sendMessage(message);
    }

    private void sendSuccessMessage(CourseDetaills result) {
        android.os.Message message = new android.os.Message();
        message.what = SUCCESS;

        Bundle data = new Bundle();
        data.putSerializable("course", result.getCourse());
        data.putString("schoolImage", result.getSchoolImage());
        data.putInt("schoolImageCount", result.getSchoolImageCount());
        message.setData(data);

        courseDetailHandler.sendMessage(message);
    }

}
